package com.hanbang.mrcm.aboutme;

import java.util.ArrayList;
import java.util.Collection;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.base.BaseActivity;
import com.hanbang.mrcm.bean.MyCdkerListData;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.JsonHelper;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.hanbang.mrcm.utils.SystemTools;
import com.hanbang.mrcm.utils.UiUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class MyCdkerActivity extends BaseActivity {

	private PullToRefreshListView listView = null;
	private ArrayList<MyCdkerListData> listDatas = new ArrayList<MyCdkerListData>();
	private MyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_cdker);
		initView();
		setOnClickListener();
		getMyCdkerData();
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {

	}

	@SuppressWarnings("unused")
	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setBackArrows(findViewById(R.id.top_bar_left_image));
		setTitle(findViewById(R.id.top_bar_title), "我的兑换码");
		listView = (PullToRefreshListView) findViewById(R.id.listview);
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
		listView.setMode(Mode.PULL_FROM_START);
		listView.getRefreshableView().setDivider(
				getResources().getDrawable(R.color.white));
		listView.getRefreshableView().setDividerHeight(
				SystemTools.dip2px(this, 10));
		listView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				listDatas.clear();
				getMyCdkerData();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {

			}
		});
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return listDatas.size();
		}

		@Override
		public Object getItem(int position) {
			return listDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MyCdkerListData data = (MyCdkerListData) getItem(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.my_cdker_list_item, null);
				holder = new ViewHolder();
				holder.cdker = (TextView) convertView.findViewById(R.id.cdker);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.time2 = (TextView) convertView.findViewById(R.id.time2);
				holder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
				holder.dhm = (TextView) convertView.findViewById(R.id.dhm);
				UiUtils.applyFont(convertView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (data.getIs_user() == 1) {
				// 未使用
				holder.cdker.setTextColor(getResources().getColor(
						R.color.red_shen));
				holder.dhm.setTextColor(getResources().getColor(
						R.color.red_shen));
				holder.ll
						.setBackgroundResource(R.drawable.red_shen_frame_my_cdker);
			} else if (data.getIs_user() == 2) {
				// 已经用过了
				holder.cdker.setTextColor(getResources().getColor(
						R.color.gray_text));
				holder.ll.setBackgroundResource(R.drawable.gray_frame_my_cdker);
				holder.dhm.setTextColor(getResources().getColor(
						R.color.gray_text));

			}
			holder.cdker.setText(data.getExchange_code());
			holder.name.setText(data.getTitle());
			String t1[] = data.getAdd_time().split(" ");
			if (t1.length > 0) {
				holder.time.setText(t1[0]);
			}
			String t2[] = data.getExpired_time().split(" ");
			if (t2.length > 0) {
				holder.time2.setText(t2[0]);
			}
			return convertView;
		}

		private class ViewHolder {
			TextView cdker;
			TextView name;
			TextView time;
			TextView time2;
			LinearLayout ll;
			TextView dhm;
		}
	}

	private void getMyCdkerData() {
		if (!isLogin(true)) {
			return;
		}
		httpUtils.send(HttpMethod.GET, HttpInterfaces.myCode + "&mobile="
				+ userData.getMobile() + "&password=" + userData.getPassword(),
				new MyRequestCallBack<String>(this,
						getString(R.string.loadding)) {
					@Override
					public void onFailure(HttpException error, String msg) {
						super.onFailure(error, msg);
						adapter.notifyDataSetChanged();
						listView.onRefreshComplete();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								Collection<MyCdkerListData> data = JsonHelper
										.parseCollection(
												json.getJSONArray("result"),
												ArrayList.class,
												MyCdkerListData.class);
								listDatas.addAll(data);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						adapter.notifyDataSetChanged();
						listView.onRefreshComplete();
					}
				});
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
